var currentCustomer; var apiMode = 0; const myShopifyDomain = window.Shopify.shop; // Makes sure to get the myshopify domain instead of the custom domain if(document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', startScript()); } else { startScript(); } /** * Gets a script from a specified source which is then added to the page in order to be * executed from the function that is passed as argument * @param {String} url The source of the script * @param {Oject} success The function that will be executed after getting the script */ function getScript(url,success){ var head = document.getElementsByTagName("head")[0], done = false; var script = document.createElement("script"); script.src = url; script.onload = script.onreadystatechange = function(){ if ( !done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete") ) { done = true; success(); } }; head.appendChild(script); } /** * The main validations are done here. * The script checks if the domain is valid and the customer ID exists, * respectively. * If both values are valid, then the token generation is executed. */ function startScript() { if (myShopifyDomain !== undefined && __st.cid !== undefined) { currentCustomer = __st.cid; getScript('https://cdn.bayonet.io/fingerprinting-2.0.min.js',function(){ initBayonet(); }); } } /** * Generates the device fingerprint token. * First it retrieves the API key from the server, if the key has * a valid value, then the generation of the token proceeds. * Once the token is generated, the script executes the callback * function to assign that token to the customer. */ async function initBayonet() { var apiData = await getApiData(); if (apiData && apiData.length === 3) { if (parseInt(apiData[0]) === 1) { apiMode = apiData[2]; _bayonet.init({ js_key: apiData[1], callback_function: "getResponse" }); _bayonet.track(); } } } /** * Gets the response from requesting the API data corresponding to the * Device Fingerprint API which includes the current active mode, * the API key for a shop and the current API mode. * It returns either an array containing the active mode, the key and * the API mode or null if an error occurs. */ async function getApiData() { var apiData = null; const url = `/tools/bayonet-fingerprint/?shopDomain=${myShopifyDomain}&requestOrigin=fingerprintScript`; const params = { method: "GET" }; await fetch(url, params) .then(function(response) { return response.json(); }) .then(function(data) { apiData = data.data; }) .catch(function(error) { console.log(error); }); return apiData; } /** * Validates if a token was actually generated; and if that is true the script * makes a call to the server to set that token to the logged in customer. * @param {Object} response The response retrieved from the token generation */ function getResponse(response) { if (response.bayonet_fingerprint_token) { const url = `/tools/bayonet-fingerprint/`; const requestBody = { "shopDomain": myShopifyDomain, "customer": currentCustomer, "token": response.bayonet_fingerprint_token, "requestOrigin": "fingerprintScript", "apiMode": apiMode }; const params = { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify(requestBody) }; fetch(url,params) .catch(function(error) { console.log(error); }); } }